package com.rxyl.love.petdoctor.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.entity.FindDoctor;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2016/1/14.
 * FindDoctor适配器
 */

public class FindDoctorAdapter<T> extends BaseAdapter {
    private Context context;
    private List<T> doctorsInfomation;

    public FindDoctorAdapter(Context context, List<T> doctorsInfomation) {
        this.context = context;
        this.doctorsInfomation = doctorsInfomation;
    }

    @Override
    public int getCount() {
        return doctorsInfomation.size();
    }

    @Override
    public Object getItem(int position) {
        return doctorsInfomation.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        T doctorIfor = doctorsInfomation.get(position);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.doctor_list_information, null);
            holder = new ViewHolder();
            holder.doctorName = (TextView) convertView.findViewById(R.id.doctor_name);
            holder.doctorHospital = (TextView) convertView.findViewById(R.id.doctor_address);
            holder.avg_socre = (TextView) convertView.findViewById(R.id.doctor_score);
            holder.doctorJob = (TextView) convertView.findViewById(R.id.doctor_level);
            holder.indroduce = (TextView) convertView.findViewById(R.id.doctor_introduce);
            holder.doctorIcon = (SimpleDraweeView) convertView.findViewById(R.id.doctor_icon);
            holder.distance = (TextView) convertView.findViewById(R.id.doctor_address_distance);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (doctorIfor instanceof FindDoctor.DataEntity.ListEntity) {
            FindDoctor.DataEntity.ListEntity entity = (FindDoctor.DataEntity.ListEntity) doctorIfor;
            holder.doctorName.setText(replaceStr(entity.getName()));
            holder.doctorHospital.setText(entity.getHospital());
            holder.doctorJob.setText(entity.getJob());
            holder.avg_socre.setText(entity.getAvg_score() + "分");
            holder.indroduce.setText(entity.getIntroduce());
            String distance
                    = new DecimalFormat("0.00").format(entity.getDistance() * 1.0 / 1000) + "km";
            holder.distance.setText(distance);
            String url = entity.getAvatar();
            holder.doctorIcon.setImageURI(Uri.parse(url));
        }

        return convertView;
    }

    public String replaceStr(String name) {
        name = name.replace("<font color=\"#00a0e9\">", "");
        name = name.replace("</font>", "");
        return name;
    }

    class ViewHolder {
        SimpleDraweeView doctorIcon;
        TextView doctorName;
        TextView doctorHospital;
        TextView doctorJob;
        TextView avg_socre;
        TextView indroduce;
        TextView distance;
    }
}
